package com.appkubes.fintech.restful.application.face.controller;


import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.common.response.ResponseCode;
import com.appkubes.fintech.restful.application.face.service.FaceAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = "人脸核身")
@RestController
@RequestMapping("face/auth")
@Slf4j
public class FaceAuthController {

    @Autowired
    private FaceAuthService faceAuthService;

    @Value("${idcard.faceauth.authSuccessUrl}")
    private String authSuccessUrl;

    @GetMapping("/getFaceUrl")
    @ApiOperation("获取人脸跳转h5 url地址")
    public Response getFaceAuthUrl(@RequestParam("userKey") String userKey) {
        return faceAuthService.getFaceAuthUrl(userKey);
    }

    @GetMapping("/callback")
//    @ApiOperation("人脸回调")
    public void callback(@RequestParam("userKey") String userKey,HttpServletResponse response) {
        Response res = faceAuthService.getFaceResult(userKey);
        String url = authSuccessUrl + "?isSuccess=" + (res.getCode()== ResponseCode.SUCCESS.getCode()?"1":"0");
        url = url + "&userKey=" + userKey;
        try {
            response.sendRedirect(url);
        }
        catch (IOException e) {
            log.error("人脸核身跳转异常", e);
        }
    }
}
